<?php

namespace app\api\controller\v1;

use app\api\model\Picture;
use app\api\service\v1\UploadService;
use app\lib\qiniu\PictureUploader;
use think\facade\Request;

class Upload
{
    protected $Service;
    public function __construct()
    {
        $this->Service = UploadService::getInstance();
    }

    /**
     * @auth('上传图片','文件管理')
     */
    public function picture()
    {
        $file = Request::file('file');
        $res = (new PictureUploader($file))->upload();
        $result = Picture::create([
            'url' => $res,
        ]);
        return writeJson(201,$result,'新增成功！');
    }


    public function base64()
    {
        $base64_code = Request::post('base64');
        $res = (new PictureUploader(''))->base64Upload($base64_code);
        $result = Picture::create([
            'url' => $res,
        ]);
        return writeJson(201,$result,'新增成功！');
    }

    public function getQiniuToken(){
        $token = (new PictureUploader(''))->getQiniuToken();
        return writeJson(201,$token,'token！');
    }

    public function pictureAdd(){
        $url = Request::post('url');
        $domain = (new PictureUploader(''))->getDomain();
        $result = Picture::create([
            'url' => $domain.$url,
        ]);
        return writeJson(201,$result,'新增成功！');
    }

    public function ossToken(){
        $result = $this->Service->ossToken();
        return writeJson(201,$result,'OSSToken！');
    }


    public function addOSSPicture(){
        $url = Request::post('url');
        $result = Picture::create([
            'url' => $url
        ]);
        return writeJson(201,$result,'新增成功！');
    }
}
